package com.weaver.smartext.core.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @Description:
 * @title: StreamUtils
 * @Author Star_Chen
 * @Date: 2023/4/3 20:22
 * @Version 1.0
 */
public class StreamUtils {

    /**
     * @return byte[]
     * @Author Star_Chen
     * @Description 将输入流转换为字节流数组，方便输入流的复用(解决了 Stream Closed、Input-stream have must > 0 bytes异常)
     * 大文件会有性能问题，这里注意超过2G会有问题
     * @Date 20:22 2023/4/3
     * @Param [inputStream]
     **/
    public static byte[] inputToByteArray(InputStream inputStream) {
        byte[] buffer = new byte[1024];
        int len;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while (true) {
            try {
                if (!((len = inputStream.read(buffer)) > -1)) break;
                baos.write(buffer, 0, len);
                baos.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return baos.toByteArray();
    }
}
